package com.creating.mict.dao;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.creating.mict.domain.Role;

/** 
 * @ClassName: RoleDao 
 * @Description: 用户角色Dao
 * @author xuelei
 * @date 2012-3-9 下午8:45:27 
 *  
 */
public class RoleDao extends HibernateDaoSupport {

	private static final Log log = LogFactory.getLog(RoleDao.class);
	
	/**
	* @Title: getRoleList 
	* @Description: 根据角色id获取用户所有角色对象
	* @param @param loginName
	* @param @return 
	* @return List<Role>    返回类型 
	* @throws
	 */
	public List<Role> getRoleList() {
		String sql = "";
		sql = " select fid, fname, fdesc, ftype, fcdate "
			+ " from tds_role " 
			+ " where ftype='2' "
			+ " order by fcdate desc "; 
		Query query = this.getSession().createSQLQuery(sql);
		List list = query.list();
		List<Role> r_list = new ArrayList<Role>();
		
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);
			Role r = new Role();
			for (int j = 0; j < obj.length; j++) {
				r.setFid("" + obj[0]);
				r.setFname("" + obj[1]);
				r.setFdesc("" + obj[2]);
				r.setFtype("" + obj[3]);
				r.setFcdateStr("" + obj[4]);
			}
			r_list.add(r);
		}
		return r_list;
	}
	
	/**
	* @Title: getRoleById 
	* @Description: 根据Id获取角色对象
	* @param @param roleId
	* @param @return 
	* @return Role    返回类型 
	* @throws
	 */
	public Role getRoleById(String roleId) {
		try {
			List<Role> list = null;
			String hql = "from Role role where role.fid='" + roleId + "'";
			list = getHibernateTemplate().find(hql);
			for (Role r : list) {
				if(r != null) {
					return r;
				}
			}
			return null;
		} catch (RuntimeException re) {
			log.error("根据角色id获取用户所有角色对象错误！", re);
			throw re;
		}
	}
	
	/**
	* @Title: addRole 
	* @Description: 保存角色
	* @param @param role
	* @param @return 
	* @return boolean    返回类型 
	* @throws
	 */
	public boolean saveRole(Role role) {
		try {
			getHibernateTemplate().saveOrUpdate(role);
			getHibernateTemplate().flush();
			return true;
		} catch (RuntimeException re) {
			log.error("保存角色失败！", re);
			//throw re;
			return false;
		}
	}
	
	/**
	* @Title: deleteRole 
	* @Description: 删除角色对象
	* @param @param role
	* @param @return 
	* @return boolean    返回类型 
	* @throws
	 */
	public boolean deleteRole(Role role) {
		try {
			getHibernateTemplate().delete(role);
			getHibernateTemplate().flush();
			return true;
		} catch (RuntimeException re) {
			log.error("删除角色对象失败！", re);
			//throw re;
			return false;
		}
	}
	
	/**
	 * 
	* @Title: clearRoleColumns 
	* @Description: 清空角色对应栏目
	* @param @param fid
	* @param @param ids
	* @param @return 
	* @return boolean    返回类型 
	* @throws
	 */
	public boolean clearRoleColumns(String fid) {
		try {
			String sql = "delete TDS_ROLE_COLUMN a " 
					   + " where a.frole='" + fid + "'";
			Query query = this.getSession().createSQLQuery(sql);
			int num = query.executeUpdate();
			if (num > 0) {
				return true;
			} else {
				return false;
			}
		} catch (RuntimeException re) {
			log.error("清空角色对应栏目失败！", re);
			throw re;
		}
	}

	/**
	 * 
	* @Title: saveRoleColumns 
	* @Description: 保存角色对应栏目
	* @param @param fid
	* @param @param ids
	* @param @return 
	* @return boolean    返回类型 
	* @throws
	 */
	public boolean saveRoleColumns(String fid, String ids) {
		try {
			//先清空
			clearRoleColumns(fid);
			//再写入
			String sql = "";
			int num = 0;
			String[] str = ids.split(",");
			for(int i = 0; i < str.length; i++) {
				if(str[i] == null || str[i].equals("")) {
					str[i] = "NULL";
				}
				sql = "insert into TDS_ROLE_COLUMN(frole, fcolumn) " 
						   + " values('" + fid + "', '" + str[i] + "')";
				Query query = this.getSession().createSQLQuery(sql);
				num = query.executeUpdate();
			}
			if (num > 0) {
				return true;
			} else {
				return false;
			}
		} catch (RuntimeException re) {
			log.error("保存角色对应栏目失败！", re);
			//throw re;
			return false;
		}
	}
	
	
}
